﻿using JobBack.Models;
using Newtonsoft.Json;
using QG.JobBack.BLL.IService;
using QG.JobBack.BLL.Service;
using QG.JobBack.DAL;
using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.ApplicationServices;
using System.Web.Mvc;

namespace JobBack.Controllers
{
    public class RoleController : Controller
    {
        private IModuleService _imoduleService;
        private IRoleService _iRoleInfoService;
        private IMenuInfoService _imenuInfoService;
        private IFunctionService _ifunctionService;
        private IModuleRoleService _imoduleRoleService;
        private IRoleFunctionPermissionService _iroleFunctionPermissionService;

        public RoleController(IRoleService iRoleInfoService, IMenuInfoService imenuInfoService, IFunctionService ifunctionService, IModuleService imoduleService, IModuleRoleService imoduleRoleService, IRoleFunctionPermissionService iroleFunctionPermissionService)
        {
            _imoduleService = imoduleService;
            _iRoleInfoService = iRoleInfoService;
            _imenuInfoService = imenuInfoService;
            _ifunctionService = ifunctionService;
            _imoduleRoleService = imoduleRoleService;
            _iroleFunctionPermissionService = iroleFunctionPermissionService;
        }

        /// <summary>
        /// 角色主页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetRoleInfoList(int page, int limit, string RoleName)
        {
            ResponseResult<RoleUserBase> result = _iRoleInfoService.GetRoleInfoList(page, limit, RoleName);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 角色添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {

            //获取所有组
            List<Module> moduleList = _imoduleService.GetModules().data;
            ViewBag.moduleList = moduleList;
            return View();
        }
        /// <summary>
        /// 添加一个角色
        /// </summary>
        /// <param name="RoleInfo"></param>
        /// <returns></returns>
        public JsonResult AddRoleInfo(RoleMoudle role)
        {
            ResponseResult<RoleInfo> result = _iRoleInfoService.AddRoleInfo(role);
            return Json(result);
        }
        /// <summary>
        /// 角色修改页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Update(int id)
        {
            //获取角色对象
            ResponseResult<RoleInfo> result = _iRoleInfoService.GetRoleInfo(id);
            //获取所有组
            List<Module> moduleList = _imoduleService.GetModules().data;
            ViewBag.moduleList = moduleList;
            //获取角色和组关联信息
            var moduleRole = _imoduleRoleService.GetModuleRole(id).data as ModuleRole;
            ViewBag.moduleRole = moduleRole;
            //获取组下的所有父级菜单
            ViewBag.moduleMenu = _imenuInfoService.GetModuleMenu(moduleRole.ModuleId.Value).data;
            //获取组下的所有功能
            ViewBag.moduleFunc = _ifunctionService.GetModuleFunction(moduleRole.ModuleId.Value).data;
            //
            ViewBag.roleFunction = _iroleFunctionPermissionService.RoleFunctionChecked(id).data;
            return View(result.data);
        }

        /// <summary>
        /// 修改一个角色
        /// </summary>
        /// <param name="RoleInfo"></param>
        /// <returns></returns>
        public JsonResult UpdateRoleInfo(string jsondata)
        {
            RoleFunctionView roleMoudle = JsonConvert.DeserializeObject<RoleFunctionView>(jsondata);
            ResponseResult<RoleMoudle> result = _iRoleInfoService.UpdateRoleInfo(roleMoudle);
            return Json(result);
        }

        /// <summary>
        /// 根据ID删除一个角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult DeleteRoleInfoById(int id)
        {
            ResponseResult<RoleInfo> result = _iRoleInfoService.DeleteRoleInfoById(id);
            return Json(result);
        }
        ///// <summary>
        ///// 查询所有父级角色
        ///// </summary>
        ///// <returns></returns>
        //public JsonResult GetParentRoleInfo()
        //{
        //    ResponseResult<RoleInfo> result = _iRoleInfoService.GetParentRoleInfo();
        //    return Json(result);
        //}

        public JsonResult UpdateStatus(int id, int status)
        {
            ResponseResult<RoleInfo> result = _iRoleInfoService.UpdateStatus(id, status);
            return Json(result);

        }
    }
}